package cclub.demo.response;

import cclub.demo.exception.SysException;
import cclub.demo.service.secretService;
import lombok.Data;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Data
@Slf4j
@Service
public class  CclubResponse<T> {

    @Autowired
    private secretService  secretService;

    /**
     * 响应代码
     */
    private Integer code = ResponseEnum.SUCCESS_CODE.getResultCode();

    /**
     * 响应消息
     */
    private String msg = ResponseEnum.SUCCESS_CODE.getResultMsg();

    /**
     * 响应数据
     */
    private T data;

    private CclubResponse(){
    }

    private CclubResponse(Integer code,String msg){
        this.code = code;
        this.msg = msg;
    }

    /**
     *返回请求失败
     */
    public  CclubResponse error(Integer code,String msg){
        CclubResponse cr = new CclubResponse(code,msg);
        cr.setData(null);
        return cr;
    }

    /**
     *返回请求成功 "\\s*|\t|\r|\n"
     */
    public  CclubResponse success(Object data){
        CclubResponse cr = new CclubResponse();
        cr.setCode(ResponseEnum.SUCCESS_CODE.getResultCode());
        cr.setMsg(ResponseEnum.SUCCESS_CODE.getResultMsg());
        try {
//           String encryptData = secretService.encryptAes(data);
//           if(StringUtils.isEmpty(encryptData)){
//               log.error("后置response加密返回数据不能为空,encryptData={},data={}",encryptData,data.toString());
//               throw new SysException("数据加密失败");
//           }
//           cr.setData(encryptData.replaceAll("\\s*|\t|\r|\n",""));
            cr.setData(data);
        }catch (Exception e){
            log.error("后置response加密返回数据出现异常data={},msg={}",data.toString(),e);
            throw new SysException("数据加密失败");
        }
        return cr;
    }

}
